//
//  WFStatusToolBar.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/14.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit

class WFStatusToolBar: UIView {
    
    // MARK:  - 持有statusViewmodel
    var statusViewModel:WFStatusViewModel?{
        didSet {
            retweetBtn.setTitle(statusViewModel?.retweeStr, forState: .Normal)
            
            commentBtn.setTitle(statusViewModel?.commentStr, forState: .Normal)
            unlikeBtn.setTitle(statusViewModel?.unlikeStr, forState: .Normal)
            print("转发数:\(statusViewModel!.retweeStr!) 评论数:\(statusViewModel!.commentStr!) 赞:\(statusViewModel!.unlikeStr!)")
        }
    }
    
    
    // MARK:  - 懒加载
    //三个控件
    private lazy var retweetBtn:UIButton = self.addChildBtn("timeline_icon_retweet", title: "转发")
    private lazy var commentBtn:UIButton = self.addChildBtn("timeline_icon_comment",title:"评论")
    private lazy var unlikeBtn:UIButton = self.addChildBtn("timeline_icon_unlike",title:"赞")
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:  初始化控件
    private func setUpUI(){
               
        retweetBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentBtn)
        }
        commentBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(retweetBtn.snp_trailing)
            make.width.equalTo(unlikeBtn)
        }
        unlikeBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(commentBtn.snp_trailing)
            make.trailing.equalTo(self)
        }
        
        let firstLine = addChildLine()
        let secondLine = addChildLine()
        
        firstLine.snp_makeConstraints { (make) -> Void in
         make.centerX.equalTo(retweetBtn.snp_trailing)
            make.centerY.equalTo(self.snp_centerY)
        }
        secondLine.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentBtn.snp_trailing)
            make.centerY.equalTo(self)
        }
    }
    
    // MARK:  - 添加line
    func addChildLine()->UIImageView{
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView
    }
    
    // MARK:  - 定义一个创建按钮的通用方法
     func addChildBtn(imgName:String,title:String)->UIButton{
        let btn = UIButton()
        btn.setImage(UIImage(named: imgName), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        //取消高亮
        btn.adjustsImageWhenHighlighted = false
        btn.setTitle(title, forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setTitleColor(UIColor.grayColor(), forState: .Normal)
        addSubview(btn)
        
        return btn
        
    }

}
